package com.ruoyi.web.controller.reserve;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reserve.domain.*;
import com.ruoyi.reserve.service.impl.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reserve/setmeal")
public class SetmealController extends BaseController {

    @Autowired
    private ISetmealService setmealService;

    /**
     * 获取岗位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Setmeal setmeal) {
        startPage();
        List<Setmeal> list = setmealService.selectSetmealList(setmeal);
        return getDataTable(list);
    }


    /**
     * 删除检查项
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{itemsIds}")
    public AjaxResult remove(@PathVariable Integer[] itemsIds) {
        return toAjax(setmealService.deleteSetmeal(itemsIds));
    }


    /**
     * 新增岗位
     */
    @Log(title = "检查项管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Setmeal setmeal) {
        if (setmealService.getInfoByCode(setmeal.getCode()) != null) {
            return AjaxResult.error("该编号已存在");
        }
        if (setmealService.getInfoByName(setmeal.getMealName()) != null) {
            return AjaxResult.error("该名称已存在");
        }
        return toAjax(setmealService.insertSetmeal(setmeal));
    }


    /**
     * 根据检查项id获取详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        return AjaxResult.success(setmealService.getInfo(id));
    }


    /**
     * 修改
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateItem")
    public AjaxResult updateItem(@RequestBody Setmeal setmeal) {

        Setmeal setmeal1 = setmealService.getInfoByCode(setmeal.getCode());
        if (setmeal1 != null) {
            if (!setmeal.getId().equals(setmeal1.getId())) {
                return AjaxResult.error("该编号已存在");
            }
        }

        Setmeal setmeal2 = setmealService.getInfoByName(setmeal.getMealName());
        if (setmeal2 != null) {
            if (!setmeal.getId().equals(setmeal2.getId())) {
                return AjaxResult.error("该名称已存在");
            }
        }
        return toAjax(setmealService.updateSetmeal(setmeal));
    }

    /**
     * 通过套餐删除其中的检查组
     */
    @Log(title = "套餐管理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteSetMeal")
    public AjaxResult deleteSetMeal(@RequestBody List<SetmealCheckGroup> setmealCheckGroups)
    {
        Integer setmeal = setmealCheckGroups.get(0).getSetmealId();
        return toAjax(setmealService.deleteGroupSetmealByIds(setmeal,setmealCheckGroups));
    }

    /**
     * 查询可以被添加进套餐的检查组
     */
    @GetMapping("/getInfoBySetMeal")
    public TableDataInfo getInfoBySetMeal(Setmeal setmeal) {
        startPage();
        List<CheckGroup> list = setmealService.getAddSetmealList(setmeal);
        return getDataTable(list);
    }

    /**
     * 新增套餐中的检查组
     */
    @Log(title = "检查组管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSetMealGroups")
    public AjaxResult addSetMealGroups(@RequestBody List<SetmealCheckGroup> setmealCheckGroupList)
    {
        return toAjax(setmealService.insertGroupSetmealByIds(setmealCheckGroupList));
    }
}
